package com.xw.service.tool.jpush;

import cn.jiguang.common.ClientConfig;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
@Service
@Transactional(rollbackFor = Exception.class)
public class JpushServiceImpl implements JPushService {

    private Logger log = Logger.getLogger(JpushServiceImpl.class);

    @Override
    public void toAllNotification(String secret,String key,String content) {
        JPushClient jpushClient = new JPushClient(secret,key, null, ClientConfig.getInstance());
        PushPayload payload = buildPushObject_all_all_alert(content);

        sendPush(jpushClient, payload);
    }

    private void sendPush(JPushClient jpushClient, PushPayload payload) {
        PushResult result;
        try {
            result = jpushClient.sendPush(payload);
            log.info(result);
            System.out.println(result);
        } catch (APIConnectionException e) {
            e.printStackTrace();
        } catch (APIRequestException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void toAllNotificationAndMessage(String secret,String key,String content,Map map) {
        JPushClient jpushClient = new JPushClient(secret,key, null, ClientConfig.getInstance());
        PushPayload payload = buildPushObject_all_all_alert_message(content,JSONObject.toJSONString(map));

        sendPush(jpushClient, payload);
    }

    @Override
    public void toAliasNotificationAndMessage(String secret,String key,String content, Map map, String... strings) {
        JPushClient jpushClient = new JPushClient(secret,key, null, ClientConfig.getInstance());
        PushPayload payload = buildPushObject_all_alias_alert_message(content,JSONObject.toJSONString(map),strings);

        sendPush(jpushClient, payload);
    }

    @Override
    public void toAliasNotification(String secret,String key,String content, String... strings) {
        JPushClient jpushClient = new JPushClient(secret,key, null, ClientConfig.getInstance());
        PushPayload payload = buildPushObject_all_alias_alert(content,strings);

        sendPush(jpushClient, payload);
    }

    /**
     * 全部通知
     * @param content
     * @return
     */
    public static PushPayload buildPushObject_all_all_alert(String content) {
        return PushPayload.alertAll(content);
    }

    /**
     * 全部通知和自定义消息
     * @param content
     * @return
     */
    public static PushPayload buildPushObject_all_all_alert_message(String content, String map) {
        return PushPayload.newBuilder()
                    .setPlatform(Platform.all())
                    .setAudience(Audience.all())
                    .setNotification(Notification.newBuilder()
                    .addPlatformNotification(AndroidNotification.newBuilder()
                            .setAlert(content)
                            .build())
                    .addPlatformNotification(IosNotification.newBuilder()
                            .setAlert(content)
                            .build())
                    .build())
                    .setMessage(Message.newBuilder()
                            .setMsgContent(content)
                            .addExtra("extra",map)
                            .build())
                .build();
    }
    /**
     * 按别名通知
     * @param content
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(String content,String... strings) {
        return PushPayload.newBuilder()
                    .setPlatform(Platform.all())
                    .setAudience(Audience.alias(strings))
                    .setNotification(Notification.newBuilder()
                    .addPlatformNotification(AndroidNotification.newBuilder()
                            .setAlert(content)
                            .build())
                    .addPlatformNotification(IosNotification.newBuilder()
                            .setAlert(content)
                            .build())
                    .build())
                .build();
    }
    /**
     * 按别名通知和自定义消息
     * @param content
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert_message(String content, String map,String... strings) {
        return PushPayload.newBuilder()
                    .setPlatform(Platform.all())
                    .setAudience(Audience.alias(strings))
                    .setNotification(Notification.newBuilder()
                    .addPlatformNotification(AndroidNotification.newBuilder()
                            .setAlert(content)
                            .build())
                    .addPlatformNotification(IosNotification.newBuilder()
                            .setAlert(content)
                            .build())
                    .build())
                    .setMessage(Message.newBuilder()
                            .setMsgContent(content)
                            .addExtra("extra",map)
                            .build())
                .build();
    }


    //测试
    public static void main(String[] args) {
        Map<String,Object> map = new HashMap<>();
        map.put("msg","透传信息");
        String string="0843ff71fd53449cbc241c9efddfa1d6";
//        new JpushServiceImpl().toAliasNotificationAndMessage(StaticConfig.JPUSH_SECRET,StaticConfig.JPUSH_KEY,"透传",map,string.split(","));
    }
}
